<?php
namespace app\mobile\controller;

use app\model\Util; 
use app\model\Theme; 
use app\model\Poster as Poster_mod;
use app\model\Worker; 
use app\model\Relation; 

class Poster extends Base
{
    public function Poster()
    {
global $_G, $_P;
$fans = $this->checkuser();
$system    = $_G['system'];
$op        = !empty($_P['op']) ? $_P['op'] : 'display';
$themeid   = intval($_P['themeid']);
if($themeid < 0){
    return_msg('活动入口不正确');
}
$theme = Theme::getThemeById($themeid);
if(empty($theme['id'])){
    die(Util::showError("请确认活动地址是否正确"));
    return;
}
$config = 	Theme::getConfig($themeid);
$joinid   = intval($_P['joinid']);
$recordid   = intval($_P['recordid']);
$type      = $_P['type'];
$forward   = $_P['forward'];
$pm = array(":themeid"=>$themeid);
$p = array(":openid"=>$_G['openid'],":themeid"=>$themeid);
if($op == 'action'){
    if($theme['status']!=1){
        $result=  array();
        $result['errcode'] = 1;
        $result['msg'] = '当前活动状态不可以参加';
        return_msg($result, '', 'ajax');
    }
    $poster =  Poster_mod::getByThemeIdnew($themeid);
    if(empty($poster['id'])){
        $result=  array();
        $result['errcode'] = 1;
        $result['msg'] = '海报未设置';
        return_msg($result, '', 'ajax');
    }
    if ($fans['status']==2) {
        $poster['bg']= tomedia($config['headlogo']);
        $poster['bg']=str_replace("/99.jpg","/haibao.jpg",$poster['bg']);
    }else{
        $poster['bg']=str_replace("/99.jpg","/haibao.jpg",$poster['bg']);
    }
    $sql = "SELECT * FROM ".tablename(t_fans)." 
            WHERE themeid = :themeid and openid = :openid";
    $myfans = db_find($sql,$p);
    if (!empty($myfans['poster']) && ($myfans['postertime']+1296000) > TIMESTAMP) {
        $result['errcode'] = 0;
        $result['msg'] = '生成成功';
        $result['img'] = $myfans['poster'];
        $result['item'] =  $qrimg ;
        return_msg($result, '', 'ajax');
    }else{
    	//员工信息
    	$mywoker = Worker::getThemeWorkerByOpenid($_G['openid'],$themeid,1);
    	if (empty($mywoker)){
    		$workerid = intval($_P['workerid']);
    		if ($workerid==0){
    			$relation = Relation::getByThemeId($themeid,$_G['openid']);
    			$workerid = $relation['workerid'];
    		}
    	}else{
            $recordid = 0;
    		$workerid = $mywoker['uid'];
    		$workername = $mywoker['username'];
    		$workermobile = $mywoker['mobile'];
    		$fans['nickname']= $mywoker['username'];
    	}

        $sharedata = array(
            'themeid'=>$themeid,
            'fopenid'=>$_G['openid'],
            'fromuid'=>$_G['member']['uid'],
    		'workerid'=>$workerid,
            'flag'=>1,
            'recordid'=>$recordid
        );
    	if (intval($_P['isyixuan'])==1){
    		$sharedata['fromuid'] = $_P['fromuid']; 
    		$sharedata['fopenid']  = $_P['fopenid'];
    	}
        
        if ($system['gongzhong'] =='1'){
            $domaintxt = "http://".$system['domainhead1'];
        }else{
            $domaintxt = $_G['url_scheme'].DOMAIN0;
        }
        $shareurl = $domaintxt."/".createMobileUrl('index',$sharedata);

        $shareurl = Poster_mod::GetPosterUrl($system,$shareurl,MODULE_NAME .'二维码'.$fans['nickname']);
        $member = array(
            "openid"   =>$_G['openid'],
            'nickname' =>$fans['nickname'],
            'avatar'   =>$fans['avatar'],
            'shareurl' => $shareurl,
            'themeid' =>$themeid,
            'addtime' => TIMESTAMP,
        );
        $md5 = md5(json_encode($member));
        $file = 'qr'.$md5 . '.png';
        $path = 'images/poster/'. date('Y-m') . '/' . MODULE_NAME.'/';
        $qrimg = $path . $file;
        //if (!is_file($qrimg)){
            //loadload_func('communication');
            //load_func('file');
            $qrimg = QRURLNEW . $shareurl;// Util::appurl('qr',MODULE_NAME,array('url'=>$shareurl));
            //$qr = QRURLNEW . urlencode($shareurl);
            // $data = yxcurl($qr);
            // if ($data[status]!='OK'){
            //     $data = yxcurl($qr); 
            // }
            // $wr = file_write('/images/poster/'. date('Y-m') . '/' . MODULE_NAME.'/'.$file, $data['content']);
            // $qrimg = 'images/poster/'. date('Y-m') . '/' . MODULE_NAME.'/'.$file;
            
            // echo $qrimg;
            // exit;
        //}
        $img = Poster_mod::createGoodsPoster($poster, $member, $qrimg);

        db_update(t_fans, array('poster' =>$img['img'],'postertime' =>TIMESTAMP), array('themeid' => $themeid, 'openid' => $_G['openid']));

        $result['errcode'] = 0;
        $result['msg'] = '生成成功';
        $result['img'] =  $img['img'] ;
        $result['item'] =  $qrimg ;
        return_msg($result, '', 'ajax');
    }
}
}
}